package com.powernode.controller;

import com.github.pagehelper.PageInfo;
import com.powernode.common.Constants;
import com.powernode.common.Result;
import com.powernode.domain.Customer;
import com.powernode.domain.User;
import com.powernode.dto.CutomerDto;
import com.powernode.service.CustomerService;
import com.powernode.utils.WebMVCUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.executor.ResultExtractor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

@Controller
@RequestMapping("customer")
@Api(tags = "客户的接口")
public class CustomerController {
    @Autowired
    private CustomerService customerService;


    //分页客户数据查询  做了处理所里不同用户查到的数据是不一样的

    @RequestMapping("list.do")
    @ResponseBody
    public Result list(CutomerDto cutomerDto) {
        //springmvc的底层都是都过set方法把对应的值set进去的,所以该实体类必须有参构造
        //把查询的数据set到实体类中去
        //data注解提供get set tostring  无参构造  但当有有参构造时就没有无参构造了
        //之前的把数据放到实体类中Customer就出现了上面的问题
        // System.out.println(cutomerDto+"-----------"+"哈哈哈");
//判断当前登录的用户是否是管理员
        User currentUser = WebMVCUtils.getCurrentUser();
        //如果是业务员，我们只能查询当前业务员
        if (currentUser.getRole().equals(Constants.ROLE_2)) {
            //设置业务员id
            cutomerDto.setUserId(currentUser.getId());
        }
        //查询数据
        PageInfo<Customer> pageInfo = this.customerService.queryCustomerForPage(cutomerDto);
        return new Result(0, "查询成功", pageInfo.getTotal(), pageInfo.getList());
    }

    //添加数据
    @ResponseBody
    @RequestMapping("addCustomer.do")
    public Result addCustomer(Customer customer) {
        try {
            //补充数据
            customer.setDeleted(Constants.DELETED_1);
            customer.setCreateTime(new Date());
            customer.setUserId(WebMVCUtils.getCurrentUser().getId());
            //保存数据
            this.customerService.insertSelective(customer);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            return Result.ADD_ERROR;
        }
    }

    //修改数据
    @ResponseBody
    @RequestMapping("/updatecustomer.do")
    public Result updatecustomer(Customer customer) {
        try {
            //保存数据
            this.customerService.updateByPrimaryKeySelective(customer);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            return Result.UPDATE_ERROR;
        }
    }

    //删除
    @ResponseBody
    @RequestMapping("/del.do")
    public Result del(Integer id) {
        try {
            Customer customer = new Customer();
            customer.setId(id);
            customer.setDeleted(Constants.ZHANGTAI_2);
            //保存数据
            this.customerService.updateByPrimaryKeySelective(customer);
            return Result.DEL_SUCCESS;
        } catch (Exception e) {
            return Result.DEL_ERROR;
        }
    }

    /**
     * 转移客户
     */
    @ResponseBody
    @RequestMapping("changeCustomerUser.do")
    public Result changeCustomerUser(Integer userId, Integer[] customerIds) {
        System.out.println(userId + "-----" + Arrays.toString(customerIds));
        try {
            this.customerService.changeCustomerUser(userId, customerIds);
            return Result.OPT_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.OPT_ERROR;
        }


    }


}
